package bmatch.tech.cloud.resource;

import bmatch.tech.cloud.auth.TokenNeeded;
import bmatch.tech.cloud.dao.FileModelMapper;
import bmatch.tech.cloud.model.FileModel;
import bmatch.tech.cloud.model.Response;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.ws.rs.GET;
import javax.ws.rs.HeaderParam;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import java.util.List;

/**
 * 云服务
 *
 * @author: Awake
 * @date: 2018-11-30 22:09
 **/
@Component
@Path("/")
public class CloudResource {

    /**
     * 日志输出
     */
    private static Logger log = LoggerFactory.getLogger(CloudResource.class);

    @Autowired
    FileModelMapper mapper;

    /**
     * 展示用户在当前路径下的文件和文件夹
     *
     * @param userId
     * @param path
     * @return
     */
    @GET
    @Path("display")
    @TokenNeeded
    @Produces(MediaType.APPLICATION_JSON)
    public Response displayAll(
            @HeaderParam("userId") String userId,
            @QueryParam("path") String path) {
        Response response = new Response();
        List<FileModel> myFileModels = mapper.select(userId, path, null);
        if (myFileModels.isEmpty()) {
            response.setCode(Response.RESPONSE_OK);
            response.setMessage("No Files And Directories.");
            response.setData(null);
            return response;
        } else {
            response.setCode(Response.RESPONSE_OK);
            response.setMessage("Display Finished.");
            response.setData(myFileModels);
            return response;
        }
    }
}

